package model

import (
	"gpt-api/framework/db"
)

type Notifications struct {
	db.BaseModel
	Title   string `json:"title" gorm:"column:title;type:varchar(150);default:'';comment:通知标题"`
	Content string `json:"content" gorm:"column:content;type:text;comment:通知内容"`
	Status  int    `json:"status" gorm:"column:status;type:int;default:0;comment:通知状态0:未读1:已读"`
	UserID  int    `json:"user_id" gorm:"column:user_id;type:int;default:0;comment:用户ID"`
}

func (Notifications) TableName() string {
	return "notifications"
}

// GetNotificationsByUserId 根据用户ID从数据库中获取通知列表
func (n *Notifications) GetNotificationsByUserId(database *db.DB, userId uint64) ([]Notifications, error) {
	var notifications []Notifications
	err := database.Model(&Notifications{}).Where("user_id = ?", userId).Find(&notifications).Error
	return notifications, err
}

// GetNotificationsById 根据通知ID从数据库中获取特定的通知
func (n *Notifications) GetNotificationsById(database *db.DB, id uint64) (Notifications, error) {
	var notifications Notifications
	err := database.Model(&Notifications{}).Where("id = ?", id).First(&notifications).Error
	return notifications, err
}

// MarkAllNotificationsRead 标记指定用户的全部通知为已读
func (n *Notifications) MarkAllNotificationsRead(database *db.DB, userId uint64) error {
	return database.Model(&Notifications{}).Where("user_id = ?", userId).Update("status", 1).Error
}

// MarkNotificationRead 标记指定通知为已读
func (n *Notifications) MarkNotificationRead(database *db.DB, id uint64, userId uint64) error {
	return database.Model(&Notifications{}).Where("id = ?", id).Where("user_id = ?", userId).Update("status", 1).Error
}

// DeleteNotification 用于删除指定用户的指定通知
func (n *Notifications) DeleteNotification(database *db.DB, id uint64, userId uint64) error {
	return database.Model(&Notifications{}).Where("id = ?", id).Where("user_id = ?", userId).Delete(&Notifications{}).Error
}
